package com.sheyu;

/**
 * @author sheyu
 * @date 2020/8/25 3:09 下午
 */
public class Index_21 {

    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null){
            return l2;
        }
        if (l2==null){
            return l1;
        }
        ListNode head = new ListNode(-1);
        ListNode ans = head;
        while(l1!=null&&l2!=null){
            if (l1.val<l2.val){
                ans.next = l1;
                l1 = l1.next;
            }else{
                ans.next = l2;
                l2 = l2.next;
            }
            ans = ans.next;
        }
        if(l2!=null){
            ans.next = l2;
        }
        if(l1!=null){
            ans.next = l1;
        }
        return head.next;
    }
}
